package com.example.xiangmuservice.controller;

import com.example.xiangmuservice.entity.User;
import com.example.xiangmuservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * MVC(V) 视图层，用于与页面交互的，做过滤和校验使用
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Integer id){
        System.out.println("我是视图层，进行过滤校验，比如验证id是否存在");
        if(id == null) {
            System.out.println("id为空，直接返回，不调用业务控制层（业务处理层）");
            return null;
        }
        User user = userService.findUserById(id);
        return user;
    }
    @GetMapping("/user") //为测试方便，通过url传参，请求应设为Get方式
    public User insertUser(User user){
        User saveUser = userService.insertUser(user);
        return saveUser;
    }

}
